package com.akuma.redmine.entity;

import com.akuma.common.util.JsonUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by akuma on 2016/10/11.
 *
 * @author akuma
 */
public class ProjectsQuery {

    private List<ProjectQuery> projects;

    private Integer total_count;

    private Integer offset;

    private Integer limit;

    public List<ProjectQuery> getProjects() {
        return projects;
    }

    public void setProjects(List<ProjectQuery> projects) {
        this.projects = projects;
    }

    public Integer getTotal_count() {
        return total_count;
    }

    public void setTotal_count(Integer total_count) {
        this.total_count = total_count;
    }

    public Integer getOffset() {
        return offset;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public String toString() {
        if (this == null) {
            return "null";
        }
        return JsonUtils.toJSon(this);
    }

    /**
     * 从关系型结构的list中获取树状结构的list
     *
     * @return
     */
    public List<ProjectQuery> getTreeProjects() {
        if (this.projects == null || this.projects.size() == 0) {
            return null;
        }
        List<ProjectQuery> destination = new ArrayList<ProjectQuery>();
        for (ProjectQuery proC : this.projects) {
            if (proC.getParent() == null) {
                continue;
            }
            for (ProjectQuery proP : this.projects) {
                if (proC.getParent().getId().equals(proP.getId())) {
                    proP.addChildren(proC);
                }
            }
        }
        for (ProjectQuery pro : this.projects) {
            if (pro.getParent() != null) {
                continue;
            }
            destination.add(pro);
        }
        return destination;
    }

    public String[] names() {
        if (this.projects == null || this.projects.size() == 0) {
            return new String[]{};
        }
        String[] names = new String[this.projects.size()];
        for (int i = 0; i < this.projects.size(); i++) {
            names[i] = this.projects.get(i).getName();
        }
        return names;
    }

    public Object[][] table() {
        if (this.projects == null || this.projects.size() == 0) {
            return new Object[][]{};
        }
        Object[][] table = new Object[this.projects.size()][];
        for (int i = 0; i < this.projects.size(); i++) {
            ProjectQuery projectQuery = this.projects.get(i);
            table[i] = new Object[]{
                    projectQuery.getId(),
                    projectQuery.getName(),
                    projectQuery.getDescription()
            };
        }
        return table;
    }
}
